﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows
Imports System.Windows.Controls
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Class FollowingPageViewModel : Inherits ObservableObject

    Private _listType As String
    Public Property ListType As String
        Get
            Return _listType
        End Get
        Set(value As String)
            SetProperty(_listType, value)
        End Set
    End Property

    Private _ic_User_Items As List(Of UserControl)
    Public Property IC_User_Items As List(Of UserControl)
        Get
            Return _ic_User_Items
        End Get
        Set(value As List(Of UserControl))
            SetProperty(_ic_User_Items, value)
        End Set
    End Property

    Private ReadOnly _client As HttpClientBase

    Public SwitchCommand As New AsyncRelayCommand(AddressOf SwitchClicked)

    Public Sub New()
        _client = New HttpClientBase()
        ListType = "我关注的"
        Call LoadDataAsync()
    End Sub

    Private Async Function LoadDataAsync() As Task
        IC_User_Items = New List(Of UserControl)
        Try
            Dim userCards As New List(Of UserControl)
            If ListType = "我关注的" Then
                'Dim userCards As New List(Of FollowingUserCard)
                Dim response = Await _client.GetAsync(Of List(Of User))("vdm/api/following/getFollowingUsers/" + Application.Current.Resources("UserId").ToString())
                If response.Code <> 200 Then
                    MessageBox.Show("获取数据失败：" + response.Message)
                End If
                If response.Data Is Nothing Then Return
                Dim users = response.Data
                For Each user In users
                    Dim card As New FollowingUserCard()
                    card.DataContext = user
                    Call card.AvatarLoad()
                    userCards.Add(card)
                Next
            Else
                'Dim userCards As New List(Of FollowingMeUserCard)
                Dim response = Await _client.GetAsync(Of List(Of User))("vdm/api/following/getFollowingMe/" + Application.Current.Resources("UserId").ToString())
                If response.Code <> 200 Then
                    MessageBox.Show("获取数据失败：" + response.Message)
                End If
                If response.Data Is Nothing Then Return
                Dim users = response.Data
                For Each user In users
                    Dim card As New FollowingMeUserCard()
                    card.DataContext = user
                    Call card.AvatarLoad()
                    userCards.Add(card)
                Next
            End If
            IC_User_Items = userCards
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show("系统错误：" + ex.Message)
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
        End Try
    End Function

    Private Async Function SwitchClicked() As Task
        If ListType = "我关注的" Then
            ListType = "关注我的"
        Else
            ListType = "我关注的"
        End If
        Await LoadDataAsync()
    End Function

End Class
